package com.kmproject.haifu.mvp.ui.adapter;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.hfinterface.HousePublishItemClickInterface;
import com.kmproject.haifu.app.hfinterface.SecondHandItemClickInterface;
import com.kmproject.haifu.app.utils.TimeUtils;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.ui.activity.HouseRentShowInfoActivity;
import com.kmproject.haifu.mvp.ui.activity.MemberCenterMainActivity;
import com.kmproject.haifu.mvp.ui.activity.SecondHandInfoShowActivity;

import java.util.List;

public class MyStorySecondHandAdapter extends BaseQuickAdapter<SecondHandInfoBean, BaseViewHolder> {
    ImageLoader glideImage;
    SecondHandItemClickInterface msecondHandItemClickInterface;
    public MyStorySecondHandAdapter(@Nullable List<SecondHandInfoBean> data,SecondHandItemClickInterface secondHandItemClickInterface) {
        super(R.layout.mystore_renthouse_item, data);
        msecondHandItemClickInterface=secondHandItemClickInterface;
    }

    @Override
    protected void convert(BaseViewHolder helper, SecondHandInfoBean item) {
        helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_gotoNext).setOnClickListener(null);
        helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_gotoNext).setClickable(false);
        helper.itemView.setOnClickListener(null);
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
        if (item.getImages()!=null&&!item.getImages().isEmpty())
        {
            glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getImages().get(0).getImageUrl())
                    .imageView(helper.<ImageView>getView(R.id.mystore_head_renthouse_item_img))
                    .build());
        }
        helper.setText(R.id.mystore_head_renthouse_item_lll,item.getViewCount()+"次浏览 "+item.getCollectionCount()+"人想要");
        helper.setText(R.id.mystore_head_renthouse_item_money,item.getSalePrice()+"P");
        if (item.getIsShow()==0)
        {
            //表示已过期的
            helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_wzs_lay).setVisibility(View.VISIBLE);
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setText(ArmsUtils.getString(mContext,R.string.mystory_shwtg_hygq_qsj));
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setTextColor(ArmsUtils.getColor(mContext,R.color.color_FF3A3A));
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setVisibility(View.GONE);//提示
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_updateLevel).setVisibility(View.VISIBLE);//升级会员提示
            helper.<TextView>getView(R.id.item_item_edit_txt).setVisibility(View.GONE);//编辑
            helper.<TextView>getView(R.id.item_item_Obtained_txt).setVisibility(View.VISIBLE);//下架
            helper.<TextView>getView(R.id.item_item_del_txt).setVisibility(View.GONE);//删除
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_updateLevel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //已过期，升级会员可以展示
                    Intent intent=new Intent(mContext, MemberCenterMainActivity.class);
                    intent.putExtra("type","house_updateMember");//从房屋跳转到升级会员界面
                    mContext.startActivity(intent);
                }
            });
            helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_gotoNext).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //未展示详情页
                    Intent intent=new Intent(mContext, SecondHandInfoShowActivity.class);
                    intent.putExtra("type","userCenter");
                    intent.putExtra("lgt","0");
                    intent.putExtra("lat","0");
                    intent.putExtra("secondHandId",item.getSecondHandId());
                    mContext.startActivity(intent);
                }
            });

        }else  if (item.getReviewStatus()==1||item.getReviewStatus()==0)
        {
            //审核中不能编辑
            helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_wzs_lay).setVisibility(View.GONE);
            helper.setText(R.id.mystore_head_renthouse_item_status,ArmsUtils.getString(mContext,R.string.mystory_shwtg_ddshz));
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setTextColor(ArmsUtils.getColor(mContext,R.color.color_3EE469));

            helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setText(ArmsUtils.getString(mContext,R.string.store_house_sjhymsh));
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setVisibility(View.VISIBLE);//提示
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_updateLevel).setVisibility(View.VISIBLE);//升级会员
            helper.<TextView>getView(R.id.item_item_edit_txt).setVisibility(View.GONE);//编辑
            helper.<TextView>getView(R.id.item_item_Obtained_txt).setVisibility(View.GONE);//下架
            helper.<TextView>getView(R.id.item_item_del_txt).setVisibility(View.GONE);//删除

            helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_layout).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArmsUtils.makeText(mContext,"正在审核中，请耐心等待！");
                }
            });
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_updateLevel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, MemberCenterMainActivity.class);
                    intent.putExtra("type", "house_updateMember");//从房屋跳转到升级会员界面
                    mContext.startActivity(intent);
                }
            });


        }else if (item.getReviewStatus()==1)
        {
            //审核未通过的
            helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_wzs_lay).setVisibility(View.GONE);
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setText(ArmsUtils.getString(mContext,R.string.mystory_shwtg_cxfb));
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setTextColor(ArmsUtils.getColor(mContext,R.color.color_FF3A3A));
            Drawable drawable= mContext.getResources().getDrawable(R.drawable.mystory_selectstory_yw_icon);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setCompoundDrawables(null,null,drawable,null);
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setVisibility(View.GONE);//提示
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_updateLevel).setVisibility(View.GONE);//升级会员提示
            helper.<TextView>getView(R.id.item_item_edit_txt).setVisibility(View.GONE);//编辑
            helper.<TextView>getView(R.id.item_item_Obtained_txt).setVisibility(View.GONE);//下架
            helper.<TextView>getView(R.id.item_item_del_txt).setVisibility(View.VISIBLE);//删除
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertView("审核未通过原因", item.getReviewDetail(), "关闭", null, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                        @Override
                        public void onItemClick(Object o, int position) {

                        }
                    },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),false).show();
                }
            });
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertView("审核未通过原因", item.getReviewDetail(), "关闭", null, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                        @Override
                        public void onItemClick(Object o, int position) {

                        }
                    },ArmsUtils.getColor(mContext,R.color.alert_button_00adfcr),false).show();
                }
            });
        }else if (item.getReviewStatus()==2){
            //审核通过
            helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_wzs_lay).setVisibility(View.GONE);
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_status).setVisibility(View.GONE);//审核状态
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setVisibility(View.VISIBLE);//提示
            helper.<TextView>getView(R.id.mystore_head_renthouse_item_updateLevel).setVisibility(View.GONE);//升级会员提示
            helper.<TextView>getView(R.id.item_item_edit_txt).setVisibility(View.VISIBLE);//编辑
            helper.<TextView>getView(R.id.item_item_Obtained_txt).setVisibility(View.VISIBLE);//下架
            helper.<TextView>getView(R.id.item_item_del_txt).setVisibility(View.GONE);//删除
            if (item.getStopEditTime().equals("0"))
            {
                //编辑时间过了，不可以编辑了
                helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setVisibility(View.GONE);//提示
                helper.<TextView>getView(R.id.item_item_edit_txt).setVisibility(View.GONE);//编辑
            }else {
                helper.<TextView>getView(R.id.mystore_head_renthouse_item_ts).setText(TimeUtils.getTime(item.getStopEditTime())+"后禁止编辑");
            }
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContext, SecondHandInfoShowActivity.class);
                    intent.putExtra("type","userCenter");
                    intent.putExtra("lgt","0");
                    intent.putExtra("lat","0");
                    intent.putExtra("secondHandId",item.getSecondHandId());
                    ArmsUtils.startActivity(intent);
                }
            });
        }

        //下架
        helper.<TextView>getView(R.id.item_item_Obtained_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                msecondHandItemClickInterface.callBack(item,null,"Obtained");
            }
        });
        //删除
        helper.<TextView>getView(R.id.item_item_del_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertView("", "确定要删除该房源？", "取消", new String[]{"确定"}, null, mContext, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == AlertView.CANCELPOSITION) {
                            //点击取消按钮

                        }else {
//                            mpresenter.removeHouseRent(item.getRentId());
                            msecondHandItemClickInterface.callBack(item,null,"del");
                        }
                    }
                }).show();
            }
        });
        //商业
        helper.setText(R.id.mystore_head_renthouse_item_title,item.getTitle());

    }
}
